package com.oxygen.pt.utils.exportUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.oxygen.pt.pojo.entity.MaintenanceDailyExcel;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 高压氧舱月维保日志导出
 */
public class MaintenanceExportUtil {

    public static void export(HttpServletResponse response,
                              List<MaintenanceDailyExcel> data) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        LocalDateTime now = LocalDateTime.now();
        String fileName = URLEncoder.encode("高压氧舱维护保养日志" + now.toString(), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MaintenanceDailyExcel.class)
                // 自适应列宽 + 手动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                // 不合并、不额外样式
                .sheet("维护保养日志")
                .doWrite(data);
    }
}